package com.itheima.mp.domain.query;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.po.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
@ApiModel(description = "分页实体")
public class PageQuery {
    @ApiModelProperty("页码")
    private int pageNo = 1;
    @ApiModelProperty("页面展示数")
    private int pageSize = 3;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc = true;

    /**
     *  1，设置分页条件
     * Page<User> page = Page.of(userQuery.getPageNo(), userQuery.getPageSize());
     * //1.1  进行条件排序
     * if(StringUtils.isNoneBlank(userQuery.getSortBy())){
     *     //排序字段不为空
     *     page.addOrder(new OrderItem(userQuery.getSortBy(), userQuery.getIsAsc()));
     * }else {
     *     //排序字段为空，以默认更新字段进行排序
     *     page.addOrder(new OrderItem("update_time",false));
     * }
     */

    public <T> Page<T> getMpPage(OrderItem ... items){
        Page<T> page = Page.of(pageNo, pageSize);
      //1.1  进行条件排序
      if(StringUtils.isNoneBlank(sortBy)){
          //排序字段不为空
          page.addOrder(new OrderItem(sortBy, isAsc));
      }else if (items != null){
          //排序字段为空，以默认排序
          page.addOrder(items);
      }
        return page;
    }

    public <T> Page<T> getMpPage(String defaultSortBy,Boolean defaultIsAsc){
        return getMpPage(new OrderItem(defaultSortBy,defaultIsAsc));
    }
    public <T> Page<T> getMpPageDefaultCreateTime(){
        return getMpPage(new OrderItem("create_time",true));
    }
    public <T> Page<T> getMpPageDefaultUpdateTime(){
        return getMpPage(new OrderItem("update_time",true));
    }
}
